package com.tensquare.spit.service;

import com.tensquare.spit.dao.SpitDao;
import com.tensquare.spit.pojo.Spit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import util.IdWorker;

import java.util.Date;
import java.util.List;
import java.util.Queue;

/**
 * @Description：TODO  吐槽业务逻辑层
 * @Author: liulei
 * @Date: 2019年10月22日
 * @Version: V1.0
 **/
@Service
public class SpitService {

    @Autowired
    private SpitDao spitDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询全部
     * @return
     */
    public List<Spit> findAll(){
        return spitDao.findAll();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public Spit findById(String id){
        return spitDao.findById(id).get();
    }

    /**
     * 保存
     * @param spit
     */
    public void save(Spit spit){
        spit.set_id(idWorker.nextId()+"");
        spit.setPublishtime(new Date());//发布日期
        spit.setVisits(0);//浏览量
        spit.setShare(0);//分享数
        spit.setThumbup(0);//点赞数
        spit.setComment(0);//回复数
        spit.setState("1");;//状态
        //如果添加的吐槽，有父节点，那么吐槽的父节点吐槽回复数要加一
        if (!StringUtils.isEmpty(spit.getParentid())){//如果存在上级ID,评论
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(spit.getParentid()));
            Update update = new Update();
            update.inc("comment", 1);
            mongoTemplate.updateFirst(query, update, "spit");
        }
        spitDao.save(spit);
    }

    /**
     * 修改
     * @param spit
     */
    public void update(Spit spit){
        spitDao.save(spit);
    }

    /**
     * 根据id删除
     * @param id
     */
    public void deleteById(String id){
        spitDao.deleteById(id);
    }

    /**
     * 根据上级ID查询吐槽数据
     * @param parentid
     * @param page
     * @param size
     * @return
     */
    public Page<Spit> findByParentid(String parentid, int page, int rows){
        Pageable pageable = PageRequest.of(page-1, rows);
        return spitDao.findByParentid(parentid, pageable);
    }

    /**
     * 吐槽点赞
     * @param spitId
     */
    public void updateThumbup(String spitId) {
        //方式一：效率有问题
      /*
        Spit spit = spitDao.findById(spitId).get();
        spit.setThumbup((spit.getThumbup()==null? 0 : spit.getThumbup())+1);
        spitDao.save(spit);
      */

        //方式二：使用原生mongon原生命令来实现自增 db.spit.update({"_id":"1"},{$inc:{thumbup:NumberInt(1)}})
        Query query = new Query();//定义条件
        query.addCriteria(Criteria.where("_id").is(spitId));//指定查询条件
        Update update = new Update();//定义更新
        update.inc("thumbup",1);
        mongoTemplate.updateFirst(query, update, "spit");
    }
}
